<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
<head>
<%@ include file="/extjs/include/all.jsp"%>
<link href="${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl/userModule/page/css/authority.css" rel="stylesheet" type="text/css">
<title>用户角色列表</title>
</head>
<body>
<div id="result" style="width:100%"></div>
</body>
</html>
<script>
Ext.onReady(function(){
	//-------------------------------------------------------------------------
	// 			下面的是表格
	//
	//-------------------------------------------------------------------------
	var userId = '<%=request.getParameter("userId")%>';	
	var sm = new Ext.grid.CheckboxSelectionModel();
	var resultWidth = Ext.get("result").getWidth();
	var cm = new Ext.grid.ColumnModel([ 
	    new Ext.grid.RowNumberer(), 
	    sm,   
	        {header:'角色ID',dataIndex:'id',width:resultWidth*0.2}, 
	        {header:'角色名称',dataIndex:'name',width:resultWidth*0.3}, 
	        {header:'描述',dataIndex:'descn',width:resultWidth*0.4}
	    ]); 
	    cm.defaultSortable = true; 
	 	var ds = new Ext.data.Store({ 
	        proxy: new Ext.data.HttpProxy({
	        	url:'${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl::/userModule/action/QueryRolewithUser.action?userId='+userId
	        }), 
	        reader: new Ext.data.JsonReader({ 
	                root: 'userRoleList' 
		    }, 
		    [ 
		        {name: 'id',mapping:'id'}, 
		        {name: 'name',mapping:'name'}, 
		        {name: 'descn',mapping:'descn'} 
		    ]), 
		    remoteSort:true 
	    }); 
	       
	    var grid = new Ext.grid.GridPanel({ 
	    el: 'result', 
	    title:'用户列表', 
	    height:600, 
	    stripeRows: true,
	    bodyStyle:'width:100%',
	    collapsible:true,
	    trackMouseOver:false, 
	    loadMask: {msg:'正在加载数据，请稍侯……'}, 
	    store:ds, 

	    cm: cm, 
	    sm:sm, 
	    tbar: [{
            text:'添加角色',
            tooltip:'添加角色',
            iconCls:'add',
            handler: function(){
            	var url = '${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl/userModule/page/roleList.jsp?userId='+userId;
            	window.location =  url;
            }
        }, '-',{
            text:'删除角色',
            tooltip:'删除角色',
            iconCls:'remove',
            handler : remove
        },'-',{
            text:'返回',
            tooltip:'返回',
            iconCls:'back',
            handler: function(){
            	//window.history.back();
            	window.location = 'userList.jsp';
            }
        }]
	});     
	ds.load(); 
    grid.render(); 	
	//--------------------------------------------------------------------------------------------
	//						删除角色
	//
	//--------------------------------------------------------------------------------------------
  	function remove(){
  	
  		arr_Records = sm.getSelections();
	    if (arr_Records.length == 0) {
	        Ext.MessageBox.alert('警告', '最少需要选择一条记录!');
	    } else {
			Ext.MessageBox.confirm('确认', '确认删除所选信息?', delRole);
		}
	}
	function delRole(btn){

        if(btn == 'yes'){
	        arr_Records = sm.getSelections(); 
	        var delStr = ''; 		 
	   	   	for (var i = 0, len = arr_Records.length; i < len; i++){    
	   	   	  	 delStr += arr_Records[i].data.id;  
	   	   	  	 delStr += ',' ;	
			} 
		    if(delStr.length > 0){
 					delStr = delStr.substring(0,delStr.length - 1) ;
			}
			Ext.Ajax.request({
			     method : 'POST',                             //发送方式.'POST'或'GET',一般是'POST'
			     url: '${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl::/userModule/action/DeleteRoleinUser.action',                  //发送到页面
			     success:function(request){                   //发送成功的回调函数
			       var myResult = Ext.util.JSON.decode(request.responseText);
			       Ext.Msg.alert('信息',myResult.message,function(){
			       		ds.load({params:{start:0, limit:20}}); 
			       });        //弹出对话框
			     },
			     failure:function(){                          //发送失败的回调函数
			       Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
			     },
			     params:{
			     		roleSerialnoObject:delStr,
			     		userId:'<%=request.getParameter("userId")%>'
			     } 
	     	});
        	
        } 
    }    
});
</script>